package com.example.androidshixi;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import java.util.ArrayList;
import java.util.List;

public class MyFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my, container, false);

        TextView myArticlesTextView = view.findViewById(R.id.my_articles_text_view);
        TextView myOrdersTextView = view.findViewById(R.id.my_orders_text_view);
        TextView shoppingCartTextView = view.findViewById(R.id.shopping_cart_text_view);
        TextView settingsTextView = view.findViewById(R.id.settings_text_view);

        myArticlesTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 获取 ArticleViewModel 实例
                ArticleViewModel articleViewModel = new ViewModelProvider(requireActivity()).get(ArticleViewModel.class);
                // 获取文章列表
                List<Article> articleList = articleViewModel.getArticleList();
                // 创建 Intent 并传递文章列表
                Intent intent = new Intent(requireContext(), MyArticlesActivity.class);
                intent.putParcelableArrayListExtra("articles", new ArrayList<>(articleList));
                startActivity(intent);
            }
        });

        myOrdersTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(requireContext(), MyOrdersActivity.class);
                startActivity(intent);
            }
        });

        shoppingCartTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(requireContext(), CartActivity.class);
                startActivity(intent);
            }
        });

        settingsTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(requireContext(), SettingsActivity.class);
                startActivity(intent);
            }
        });

        return view;
    }
}